<template>
	<view class="confirm-order">
		<block v-if="detail!=''">
		<view class="inner">
			<view class="goods-row-top">
				<view class="head-container">
					<image :src="detail.head_pic" class="head-pic" mode="widthFix"></image>
					<text>{{detail.mobile}}</text>
				</view>
				<view class="rows">
					<view class="row">
						<text class="left">出售</text>
						<text class="right">{{detail.name}}{{detail.money}}张</text>
					</view>
					<view class="row">
						<text class="left">单价</text>
						<text class="right">{{detail.jy_price}}元</text>
					</view>
					<view class="row">
						<text class="left">总价</text>
						<text class="right">{{detail.amount}}元</text>
					</view>
				</view>
			</view>
			<view class="goods-row">
				<view class="goods-row-tit">请选择支付方式</view>
				<view class="pay-items">
					<view @tap="changePay(item.code)" class="pay-item" v-for="(item,index) in pay_list" :key="index">
						<image mode="widthFix" :src="`https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/order/zf-${item.code}.png`" class="pay-item-left-img"/>
						<text>{{item.name}}</text>
						<image mode="widthFix" :src="form.pay_code==item.code?'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox-sel.png':'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox.png'" class="pay-item-img"/>
					</view>
				</view>
			</view>
			<view style="height: 120rpx;"></view>
		</view>
		<view class="confirm-order-bot">
			<view class="left">
				<text>合计:</text>
				<text class="amount">{{detail.amount}}</text>
				<text v-if="false">元</text>
			</view>
			<button type="default" class="zf-btn" :disabled="btnDisabled" @click="tijiao()">立即支付</button>
		</view>
		</block>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				form:{
					step:1,
					id:0,
					pay_code:''
				},
				temp_index:-1,
				btnDisabled:false,
				detail:'',
				pay_list:[]
			};
		},
		onLoad(e) {
			_this=this;
			if(e.id){
				_this.form.id=e.id
			}
			if(e.index){
				_this.temp_index=e.index
			}
			_this.jy_buy()
		},
		methods:{
			changePay(code){
				if(this.form.pay_code==code){
					return;
				}
				this.form.pay_code=code
			},
			tijiao(){
				_this.form.step=2
				_this.jy_buy()
			},
			async jy_buy() {
				if(_this.form.step==2){
					_this.btnDisabled=true
				}
				const data = await _this.$post('api/jy_buy',_this.form)
				if(data.code==100){
					uni.$u.toast(data.msg)
					setTimeout(()=>{
						uni.navigateBack({})
					},2000)
				}else if(data.code==200){
					uni.$u.toast(data.msg)
					uni.$emit('dealBuy',_this.temp_index)
					setTimeout(()=>{
						uni.navigateBack()
					},2000)
				}else if(data.code==30){
					uni.requestPayment({
					    provider: 'wxpay',
						timeStamp: data.data.timeStamp+'',
						nonceStr: data.data.nonceStr,
						package: data.data.package,
						signType: data.data.signType,
						paySign: data.data.sign,
						success: function (res) {
							console.log('success:' + JSON.stringify(res));
							_this.$u.toast('支付成功')
							setTimeout(res=>{
								uni.$u.route({
									url:'pages/foster/foster-order-list',
									type:'redirectTo',
									params:{
										currentIndex:1
									}
								})
							},2000)
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err));
						}
					});
				}else if(data.code==10){
					this.detail = data.data.detail
					this.pay_list = data.data.pay_list
					this.form.pay_code=this.pay_list[0]['code']
				}else{
					uni.$u.toast(data.msg)
				}
				setTimeout(()=>{
					_this.btnDisabled=false
				},2000)
			}
		},
		onShow() {},
	}
</script>

<style lang="scss" scoped>
	@import "@/static/css/order.scss";
	.confirm-order{
		.inner{}
	}
</style>
